package com.xianyv.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xianyv.common.Result;
import com.xianyv.dto.EmployeeDto;
import com.xianyv.entity.Employee;
import com.xianyv.entity.Salary;
import com.xianyv.service.EmployeeService;
import com.xianyv.service.SalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SalaryService salaryService;

    /**
     * 新增员工
     * @param employeeDto
     * @return
     */
    @Transactional
    @PostMapping("/save")
    public Result save(@RequestBody EmployeeDto employeeDto){
        log.info("employeeDto:{}",employeeDto);

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto,employee, Employee.class);
        employee.setCreateTime(LocalDate.now());
        Salary salary = employeeDto.getSalary();
        salary.setEmployeeId(employee.getId());

        employeeService.save(employee);

        boolean save = salaryService.save(salary);
        return save ? Result.success(null) : Result.error("系统发生错误!!!");
    }

    /**
     * 修改员工信息
     * @param employeeDto
     * @return
     */
    @Transactional
    @PutMapping
    public Result update(@RequestBody EmployeeDto employeeDto){
        log.info("employeeDto:{}",employeeDto);

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto,employee, Employee.class);
        employee.setCreateTime(LocalDate.now());
        Salary salary = employeeDto.getSalary();

        employeeService.update(employee,new LambdaQueryWrapper<Employee>().eq(Employee::getId,employee.getId()));
        boolean update = salaryService.update(salary, new LambdaUpdateWrapper<Salary>().eq(salary != null,Salary::getEmployeeId, employee.getId()));

        return update ? Result.success(null) : Result.error("修改失败!!!");

    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @Transactional
    @DeleteMapping("/delete/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("ids:{}",ids);

        if(!employeeService.removeBatchByIds(ids)){
            return Result.error("删除失败!!!");
        }
        boolean b = salaryService.removeBatchByIds(ids);

        return b ? Result.success(null) : Result.error("删除失败!!!");
    }


    /**
     * 分页查询和搜索
     * @param pageNo
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result<Page<Employee>> getEmployee(int pageNo,int pageSize,String search){
        Page<Employee> page = new Page<>(pageNo,pageSize);

        boolean b = search != null && !"".equals(search);

        if (!b){
            page = employeeService.page(page);
        }else {
            page = employeeService.page(page,new LambdaQueryWrapper<Employee>().like(Employee::getId,search)
                    .or().like(Employee::getName,search).or().like(Employee::getDepartment,search));
        }
        return Result.success(page);
    }
}
